<template>
	<view>
		<van-card v-for="item in list" :key="item.objectId" :price="item.price" :desc="item.xiangqing"
			:title="item.xiangqing" :thumb="item.image">
			<view slot="footer">
				<van-button @click="showPopup(item.objectId)" size="mini">修改价格</van-button>
				<van-popup class="popup" v-model="show" :show=show>
					<view class="pricetext">更新价格：</view> <input class="priceinput" type="text" v-model="inputValue" />
					<van-button class="button" type="primary" @click="onConfirm">确定</van-button>
					<van-button @click="onCancel">取消</van-button>
				</van-popup>
				<van-button @click="delectsp(item.objectId)" size="mini">删除商品</van-button>
				<van-popup class="popup" v-model="showoff" :show=showoff>
					<view>确定要删除商品么</view>
					<van-button class="button" type="primary" @click="onConFirm(item.objectId)">确定</van-button>
					<van-button @click="onCanCel">取消</van-button>
				</van-popup>

			</view>
			<hr />
		</van-card>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				show: '',
				showoff: '',
				inputValue: '',
				id: ''
			}
		},
		methods: {
			data() {
				const id = uni.getStorageSync("username")
				let url = `/1.1/classes/bscs?where={"people_id":"${id}"}`
				this.$get(url).then(res => {
					uni.stopPullDownRefresh()
					// console.log(res)
					let {
						results
					} = res
					// if(results.length){
					this.list = this.list.concat(res.results)


					// console.log(this.list)
					console.log(res)

					// }
					// console.log(this.list)
				})
				// console.log(this.list)
				// console.log(this.value)
			},

			showPopup(key) {
				this.show = true;
				console.log(key)
				this.id = key
			},
			onConfirm() {
				// 点击确定按钮的逻辑
				console.log('输入的内容为：', this.inputValue);
				// 定义请求的URL
				var url = 'https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/bscs/' + this.id;

				// 定义请求的Header
				var header = {
					"X-LC-Id": "r5CTN9z96pCW8mI8fx9YxOmw-gzGzoHsz",
					"X-LC-Key": "8gSsl4Sz0ILMiQeMNMA8xQqg",
					'Content-Type': 'application/json'
				};

				// 定义请求的数据
				var data = {
					// 要修改的数据
					price: this.inputValue
				};

				// 发送请求
				uni.request({
					url: url,
					method: 'PUT',
					header: header,
					data: data,
					success: function(res) {
						// 请求成功的处理逻辑
						console.log(res.data.objectId);
						uni.request({
							url: `https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/food?where={"Commodity_id":"${res.data.objectId}"}`,
							method: 'GET',
							header: header,

							success: function(res) {
								// 请求成功的处理逻辑

								console.log(res.data);
								if (res.data.results.length > 0) {
									for (let i = 0; i < res.data.results.length; i++) {
										uni.request({
											url: `https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/food/` +
												res.data.results[i].objectId,
											method: 'PUT',
											header: header,
											data: data,
											success: function(res) {
												// 请求成功的处理逻辑

												console.log(res.data);
												uni.redirectTo({
													url: '/pages/myhome/myhome'
												})

											},
											fail: function(err) {
												// 请求失败的处理逻辑
												console.log(err);
											}
										});
									}
								}

							},
							fail: function(err) {
								// 请求失败的处理逻辑
								console.log(err);
							}
						});
					},
					fail: function(err) {
						// 请求失败的处理逻辑
						console.log(err);
					}
				});
				console.log("修改价格触法了")

				this.show = false;
			},
			onCancel() {
				// 点击取消按钮的逻辑
				console.log('点击了取消按钮');
				this.show = false;
			},
			delectsp(key) {
				this.showoff = true;
				this.id = key
			},
			onConFirm(key) {
				// 点击确定按钮的逻辑
				// console.log('输入的内容为：', this.inputValue);
				// 定义请求的URL
				var url = 'https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/bscs/' + this.id;

				// 定义请求的Header
				var header = {
					"X-LC-Id": "r5CTN9z96pCW8mI8fx9YxOmw-gzGzoHsz",
					"X-LC-Key": "8gSsl4Sz0ILMiQeMNMA8xQqg",
					'Content-Type': 'application/json'
				};

				// 定义请求的数据


				// 发送请求
				uni.request({
					url: url,
					method: 'DELETE',
					header: header,

					success: function(res) {
						// 请求成功的处理逻辑
						console.log(res);
						// console.log(this.id)
						let Id=key
						uni.request({
							url: `https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/food?where={"Commodity_id":"${key}"}`,
							method: 'GET',
							header: header,

							success: function(res) {
								// 请求成功的处理逻辑
console.log(res)
								console.log(res.data);
								if (res.data.results.length > 0) {
									for (let o = 0; o < res.data.results.length; o++) {
										uni.request({
											url: `https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/food/` +
												res.data.results[o].objectId,
											method: 'DELETE',
											header: header,

											success: function(res) {
												// 请求成功的处理逻辑

												console.log(res.data);
												uni.switchTab({
													url: '/pages/myhome/myhome'
												})

											},
											fail: function(err) {
												// 请求失败的处理逻辑
												console.log(err);
											}
										});
									}
								}

							},
							fail: function(err) {
								// 请求失败的处理逻辑
								console.log(err);
							}
						});
					},
					fail: function(err) {
						// 请求失败的处理逻辑
						console.log(err);
					}
				});
				console.log("商品删除触法了")

				this.showoff = false;
			},
			onCanCel() {
				console.log('点击了取消按钮');
				this.showoff = false;
			}
			
		},
		onLoad() {
			this.data()
		}
	}
</script>

<style>
	.priceinput {
		width: 100px;
		border: 1px solid black;
		height: 30px;
		font-size: 18px;
		float: left;
	}

	.pricetext {
		font-size: 18px;
		float: left;
	}

	.popup {
		/* width: 400px; */
	}

	.button {
		margin-top: 30px;
	}
</style>